Create the ColorModelServiceImpl
source:
package colorservice;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import model.Color;
import model.ColorManager;
@Path(value = "/colorservice")
public class ColorModelServiceImpl implements ColorModelService {
// private ColorManager colorManager = null;
// Trying a static property - not sure that I'd use this for a 'real' application.
// see other options below under 'getColoManager()'
private static ColorManager colorManager = null;
public ColorModelServiceImpl() {
System. out.println( "*** ColorModelServiceImpl()");
}
public void startUp() {
System. out.println( "*** ColorModelServiceImpl.startUp CALLED");
}
public void shutdown() {
System. out.println( "*** ColorModelServiceImpl.shutdown CALLED");
System. out.println( "*** setting colorManager to NULL");
ColorModelServiceImpl. colorManager = null;
}
public ColorManager getColorManager() {
if (ColorModelServiceImpl. colorManager == null) {
System. out.println( "*** ColorModelServiceImpl.getColors().colorManager is NULL! ");
}
// ----------------------
if (ColorModelServiceImpl. colorManager == null) {
System. out.println( "*** Setting colorManager with osgi service lookup");
try {
InitialContext ic = new InitialContext();
// this.colorManager = (ColorManager) ic.lookup("osgi:service/model.ColorManager");
// The next two methods of lookup work with Liberty v8.5.0.2
// this.colorManager = (ColorManager) ic.lookup("osgi:service/"+ColorManager.class.getName());
ColorModelServiceImpl. colorManager = (ColorManager) ic.lookup("blueprint:comp/colormanagerservice" );
} catch (NamingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// ----------------------
return colorManager;
}
public void setColorManager(ColorManager colorManager) {
System. out.println( "*** ColorModelServiceImpl.setColorManager()");
ColorModelServiceImpl. colorManager = colorManager;
}
@Override
@GET
@Path("stub")
@Produces("application/json")
public ColorList getColorsSTUB() {
ColorList colorList = new ColorList();
List<Color> list = new ArrayList<Color>();
list.add( new Color( "blue", "blue"));
list.add( new Color( "green", "green"));
list.add( new Color( "purple", "purple"));
colorList.setColorList(list);
return colorList;
}
@Override
@GET
@Path("getcolors")
@Produces("application/json")
public ColorList getColors() {
ColorManager cMgr = getColorManager();
List<Color> list = cMgr.getColor();
ColorList colorList = new ColorList();
colorList.setColorList(list);
return colorList;
}
}
---------------------------------------------
ColorModelServiceImpl Review:
NOTE: Only covering the interesting bits
---------------------------------------------
private static ColorManager colorManager = null;
Both CXF and Wink (JAX-RS implementations) create a new ColorModuleServiceImpl per each invocation. Though I've read of a few methods, via blueprint, in guiding proper managed startup... I haven't been able to make them work. I listed three work-arounds in source:
1) use a static colorManager property
2) InitialContext().lookup("osgi:service/model.ColorManager")
3) InitialContext().lookup("blueprint:comp/colormanagerservice")
#3 is the IBM's recommended approach
I choose #1 for simplicity - though, I probably wouldn't go with this approach normally and prefer to pull instances from some sort of a managed pool of ready connections.
------------------------------------------------
public ColorManager getColorManager()
Normally this simply returns the static property colorManager. Earlier however, I was having some trouble getting Wink to use my "officially" blueprint managed ColorModelService instance. The method's details illustrates other methods for working around object lookup.
------------------------------------------------
public void setColorManager(ColorManager colorManager)
Blueprint requires the setter for dependency wiring. As blueprint creates a "managed" bean, this property is automatic set via "property reference" (see below details under "blueprint").
------------------------------------------------
public ColorList getColorsSTUB ()
This test STUB returns a fully constructed color list without needing a JPA connection into our database.
public ColorList getColorsSTUB() {
ColorList colorList = new ColorList();
List<Color> list = new ArrayList<Color>();
list.add( new Color( "blue", "blue"));
list.add( new Color( "green", "green"));
list.add( new Color( "purple", "purple"));
colorList.setColorList(list);
return colorList;
}
------------------------------------------------
public ColorList getColors()
The primary responsibility for this class is to fetch colors from the database via JPA
public ColorList getColors() {
ColorManager cMgr = getColorManager(); // gets a reference to the color manager (JPA entity wrapper)
List<Color> list = cMgr.getColor(); // fetch a list of color entities
ColorList colorList = new ColorList(); // create ColorList wrapper - required for JSON/XML construction
colorList.setColorList(list); // set list into the container
return colorList;
}
Add ColorModelServiceImpl.class to AppService
package colorservice;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class AppService extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
// URL http://babe:9080/osgi_jaxrs_03a/jaxrs/servicetest
classes.add(ServiceTest. class);
// http://babe:9080/osgi_jaxrs_03a/jaxrs/colorservice/stub
// http://babe:9080/osgi_jaxrs_03a/jaxrs/colorservice/getcolors
classes.add(ColorModelServiceImpl.class);
return classes;
}
}
2) Configure Blueprint
Create blueprint.xml
right-click project -> select "other" -> filter on "bue" -> select Blueprint File"
-> Next
New Blueprint File
Select default location for this OSGi type of project
"osgi_jaxrs_03a/WebContent/OSGI-INF/blueprint"
-> next
Add blueprint namespaces
-> Finish
Create a "Bean"
Select "Blueprint" -> click "add"
Add a Bean
Select "Bean" click "OK"
New Blueprint Bean
click "Browse" -> filter on "colorservice." -> select ColorModelServiceImpl from osgi_jaxrs_03a project
NOTE: Make sure you select the class from the correct project !
Create new blueprint bean
Leave default recommendation for Bean ID
-> OK
Bean Created
Create Reference
Select "blueprint" -> click "add"
Select Reference
Select "Reference" -> click "OK"
Select Interface to Reference
Don't forget we're adding a reference into osgi_jaxrs_03a (JAX-RS) project
-- We're referencing an interface within the osgi_jpa_03 (JPA) project
Click "Browse" -> select ColorManager interface from osgi_jpa_03 project
-> OK
Leave Component name blank!
Bean and Reference created from osgi_jaxrs_03a project
Reviewing references
The bean class points to colorservice.colorModelServiceImpl
The reference interface points to model.ColorManager interface within osgi_jpa_03 project
Property colorManager points to reference ColorModelServiceImplBeanReference
Create a Property for ColorModelServiceImplBean
Select ColorModelServiceImplBean -> click Add
-> Select Property -> click OK
Set Property Details
select the property we just created -> Details Reference Browse ->
-> select Reference: ColorModelServiceImplBeanReference -> OK
Set Property Details (continued)
Set Name -> colorManager
NOTE: "colorManager" the property from class ColorModelServiceImpl (see above code review)
Blueprint property requires a setter to set value (wire) ColorManager to ColorModelService (ReST API)
Create Reference Listener for ColoModelServiceImplBeanReference
Select referecne -> click "add" -> select Reference listener
-> OK
Fill in Reference Listener Details
Click Browse -> select Reference (created above) -> OK
Reference Listener Method References
Fill in method names:
Bind: referenceBind
Unbind: referenceUnBind
Reference Listener methods point to ColorManagerImpl methods (from JPA project)
3) Create OSGi Application and Deploy to Liberty Server
Create OSGi Application Project
Add projects:
- osgi_jpa_03 (jpa project)
- osgi_jaxrs_03a (JAX-RS project)
Deploy project to Liberty Server and Start
Log output: messages.log
Right-click service and copy/paste url into browser
Test Works
==============
DONE
==============